package com.apes.crm.vip.masterdata.service;

import com.apes.crm.vip.masterdata.model.VipLabelType;
import com.apes.crm.vip.masterdata.repository.VipLabelTypeRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能：会员类型 Service
 *
 * @author HuJianlin
 * @create 2018-03-15 14:35
 */
@Service("vipLabelTypeService")
public class VipLabelTypeService extends DomainService {

    @Autowired
    VipLabelTypeRepository vipLabelTypeRepository;

    public VipLabelType save(SimpleRequest request) {
        VipLabelType vipType = request.getO(VipLabelType.class);
        vipLabelTypeRepository.saveAndFlush(vipType);
        return vipType;
    }

    public VipLabelType update(SimpleRequest request) {
        String id = request.get("id");
        int version = request.get("version");

        // 从数据库中取数，检查数据
        VipLabelType oldVipType = vipLabelTypeRepository.findOne(id);
        if (oldVipType == null) throw new RuntimeException("会员类型【" + id + "】在系统中不存在。");
        if (oldVipType.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");

        VipLabelType vipType = request.getO(VipLabelType.class);
        return vipLabelTypeRepository.saveAndFlush(vipType);
    }

    public List<VipLabelType> findAll(SimpleRequest request) {
        List<VipLabelType> vipTypes = vipLabelTypeRepository.findAll();
        return vipTypes;
    }

}
